/**
 * 
 */
package com.app.mvc.action.test;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.DateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.app.commons.exceltool.ExcelTool;
import com.app.service.ICornDBService;
import com.app.service.IResultDnaPoint;
import com.app.service.vo.CornDbDnaVo;
import com.app.service.vo.DNAVo;

/**
 * @author yuxk
 * 
 */
@Controller
@RequestMapping("/file")
public class FileUpdateAction {

	/**
	 * 
	 */
	public FileUpdateAction() {
		// TODO Auto-generated constructor stub
		log.info("com.app.mvc.action.test.FileUpdateAction loader");
	}

	private static Logger log = Logger.getLogger(FileUpdateAction.class);

	/**
	 * 处理文档模板
	 */
	@Autowired
	private ExcelTool exceltool;

	/**
	 * 获取具体的dna解析结果
	 */
	@Autowired
	private IResultDnaPoint dnapoint;
	
	@Autowired
	private ICornDBService corndbservice;

	/**
	 * 处理excel文件上传 文件类型要提取出来
	 * 
	 * @param request
	 * @return
	 */
	@SuppressWarnings({ "unchecked" })
	@RequestMapping("/up.form")
	public String fileuploader(HttpServletRequest request) {
		log.info("excel上传开始....");
		DiskFileItemFactory factory = new DiskFileItemFactory();
		ServletFileUpload fu = new ServletFileUpload(factory);
		String excelpath = request.getSession().getServletContext()
				.getRealPath("")
				+ "\\excelroot\\";
		DateFormat df = DateFormat.getDateTimeInstance(2, 1);
		InputStream input = null;
		List<FileItem> filelist = null;
		try {
			filelist = fu.parseRequest(request);

			if (null != filelist) {
				for (FileItem fi : filelist) {
					System.out.println(fi.getName());
					if (!fi.isFormField()
							&& (fi.getName().endsWith("xlsx") || fi.getName()
									.endsWith("xls"))) {
						String filename = fi.getName();
						input = fi.getInputStream();
						String endfilename = excelpath + df.format(new Date())
								+ " " + filename;
						FileOutputStream outputStream = new FileOutputStream(
								endfilename);
						byte[] b = new byte[input.available()];
						input.read(b);
						outputStream.write(b);
						outputStream.close();
						DNAVo dnavo = (DNAVo) this.dnapoint.getDNAVo(
								new FileInputStream(endfilename), "corn");
						log.info(dnavo);
					}
				}
			}
		} catch (FileUploadException e) {
			// TODO Auto-generated catch block
			log.info("文件上传失败");
			log.error(e);
			return "redirect:/welcome.jsp";
		} catch (IOException e) {
			// TODO Auto-generated catch block
			log.info("文件上传失败");
			log.error(e);
			return "redirect:/welcome.jsp";
		}
		log.info("excel上传成功....");
		return "/service/uploader-scuess";
	}

	/**
	 * 模板下载
	 * 
	 * @param key
	 *            要下载的模板名称
	 * @return
	 */

	@RequestMapping(value = "/downmb.form")
	public String dowmMb(@RequestParam("modle-key") String key,
			HttpServletResponse response) {
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/x-download");
		response.setHeader("Content-disposition", "inline; filename=" + key
				+ ".xls");
		OutputStream output = null;
		try {
			output = response.getOutputStream();
			byte[] b = this.exceltool.getExcel(key);
			output.write(b);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return null;
	}

	/**
	 * 根据指定名称下载文件
	 * 
	 * @param filename
	 * @return
	 */
	@RequestMapping(value = "/downfilebyname.form")
	public String dowmTest(@RequestParam(value = "id") String id,
			HttpServletResponse response, HttpServletRequest request) {
		CornDbDnaVo vo = this.corndbservice.getCornDbDnaVoById(id);
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/x-download");
		response.setHeader("Content-disposition", "inline; filename="
				+ vo.getFilePath() );
		OutputStream output = null;
		String excelpath = request.getSession().getServletContext()
				.getRealPath("")
				+ "\\excelroot\\";
		try {
			InputStream input = new FileInputStream(excelpath+vo.getFilePath());
			byte b[] = new byte[input.available()];
			input.read(b);
			input.close();
			output = response.getOutputStream();
			// byte[] b = this.exceltool.getExcel(key);
			 output.write(b);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	public ExcelTool getExceltool() {
		return exceltool;
	}

	public void setExceltool(ExcelTool exceltool) {
		this.exceltool = exceltool;
	}

	public IResultDnaPoint getDnapoint() {
		return dnapoint;
	}

	public void setDnapoint(IResultDnaPoint dnapoint) {
		this.dnapoint = dnapoint;
	}

	public ICornDBService getCorndbservice() {
		return corndbservice;
	}

	public void setCorndbservice(ICornDBService corndbservice) {
		this.corndbservice = corndbservice;
	}

	
}
